import { onMounted, onBeforeUnmount } from 'vue'

export function useResizeObserver(el: () => HTMLElement | null, cb: () => void) {
  let ro: ResizeObserver | null = null

  onMounted(() => {
    const target = el()
    if (target) {
      ro = new ResizeObserver(cb)
      ro.observe(target)
    }
  })

  onBeforeUnmount(() => ro?.disconnect())
}
